import 'package:flutter/material.dart';
import 'package:flutter_billi/model/home_mo.dart';
import 'package:flutter_billi/model/video_model.dart';
import 'package:flutter_billi/util/view_util.dart';

class ExpandedContent extends StatefulWidget {
  final VideoModel mo;
  const ExpandedContent({Key key, @required this.mo}) : super(key: key);

  @override
  _ExpandedContentState createState() => _ExpandedContentState();
}

class _ExpandedContentState extends State<ExpandedContent> with SingleTickerProviderStateMixin {
  static final Animatable<double> _easeInWeen = CurveTween(curve: Curves.ease);
  // 默认情况下 关闭展开状态
  bool _expand = false;

  AnimationController _controller;

  Animation<double> _heightFactor;

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(vsync: this, duration: Duration(milliseconds: 200));

    _heightFactor = _controller.drive(_easeInWeen);

    _controller.addListener(() {
      print(_heightFactor);
    });
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.only(left: 15, right: 15, top: 5),
      child: Column(
        children: [
          _buildTitle(),
          Padding(padding: EdgeInsets.only(left: 8)),
          _buildInfo(),
          _buildDes(),
        ],
      ),
    );
  }

  _buildTitle() {
    return InkWell(
      onTap: _toggleExpand,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Expanded(
              child: Text(
            widget.mo.title,
            maxLines: 1,
            overflow: TextOverflow.ellipsis,
          )),
          Padding(
            padding: EdgeInsets.only(left: 15),
            child: Icon(
              _expand ? Icons.keyboard_arrow_up_sharp : Icons.keyboard_arrow_down_sharp,
              color: Colors.grey,
              size: 16,
            ),
          )
        ],
      ),
    );
  }

  void _toggleExpand() {
    setState(() {
      _expand = !_expand;
      if (_expand) {
        _controller.forward();
      } else {
        _controller.reverse();
      }
    });
  }

  _buildInfo() {
    var style = TextStyle(fontSize: 12, color: Colors.grey);
    var dataStr = widget.mo.createTime.length > 10 ? widget.mo.createTime.substring(5, 10) : widget.mo.createTime;
    return Row(
      children: [
        ...smallIconText(Icons.ondemand_video, widget.mo.view),
        Padding(padding: EdgeInsets.only(left: 10)),
        ...smallIconText(Icons.list_alt, widget.mo.reply),
        Text(
          "$dataStr",
          style: style,
        )
      ],
    );
  }

  _buildDes() {
    var child = _expand
        ? Text(
            widget.mo.desc,
            style: TextStyle(fontSize: 12, color: Colors.grey),
          )
        : null;
    return AnimatedBuilder(
      animation: _controller.view,
      child: child,
      builder: (BuildContext context, Widget child) {
        return Align(
          heightFactor: _heightFactor.value,
          alignment: Alignment.topCenter,
          child: Container(
            alignment: Alignment.topLeft,
            padding: EdgeInsets.only(top: 8),
            child: child,
          ),
        );
      },
    );
  }
}
